package com.website.system.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.website.common.controller.BaseController;
import com.website.common.warpper.ResultMap;
import com.website.system.model.ComputerCategory;
import com.website.system.model.ComputerMethod;
import com.website.system.model.vo.ComputerMethodVO;
import com.website.system.service.ComputerCategoryService;
import com.website.system.service.ComputerMethodService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("manage/computerMethod")
public class ComputerMethodController extends BaseController {

    @Autowired
    private ComputerMethodService computerMethodService;

    @Autowired
    private ComputerCategoryService computerCategoryService;


    /**
     * 跳转到 后台-->资源管理中心
     */
    @RequestMapping("")
    public String computer() {
        return "manage/computerMethod";
    }

    /**
     * 加载数据 后台-->分类管理
     */
    @ApiOperation("分类 管理JSON数据")
    @ResponseBody
    @RequestMapping("/computerMethodTable")
    public ResultMap methodTable(@RequestParam Integer page, @RequestParam Integer limit) {

        ResultMap<List<ComputerMethodVO>> resultMap = new ResultMap<>(SUCCESS_CODE, SUCCESS_MSG, null, null);
        Wrapper<ComputerMethod> wrapper = new EntityWrapper<>(); //条件构造器
        Page<ComputerMethod> pageInfo = new Page<>(page,limit); //实例化page

        List<ComputerMethod> computerMethod = computerMethodService.selectPage(pageInfo, wrapper.orderBy("create_time", false)).getRecords();
        resultMap.setCount(computerMethodService.selectCount(wrapper));

        List<ComputerMethodVO> voList = new ArrayList<>();  //实例化Vo类
        for (ComputerMethod computer:computerMethod) {
            ComputerMethodVO computerMethodVO =new ComputerMethodVO();
            if(!computer.getMid().equals("null")){
                    ComputerCategory  computerCategory=computerCategoryService.selectById(computer.getMid());
                    computerMethodVO.setId(computer.getId());
                    computerMethodVO.setMid(computer.getMid());
                    computerMethodVO.setName(computer.getName());
                    computerMethodVO.setCreateTime(computer.getCreateTime());
                    computerMethodVO.setModifiedTime(computer.getModifiedTime());
                    computerMethodVO.setType(computerCategory.getType());
            }
            voList.add(computerMethodVO);
        }
        resultMap.setData(voList);
        return resultMap;
    }
    /**
     * 加载电脑类别
     */
    @ResponseBody
    @RequestMapping(value = "/ComputerMethodType", method = RequestMethod.POST)
    public List<ComputerCategory> queryDoctorPosition() {
        Wrapper<ComputerCategory> w = new EntityWrapper<>();
        return computerCategoryService.selectList(w);
    }

    @RequestMapping("/toComputerMethodAdd")
    public String toComputerAdd() {
        return "manage/computerMethodAdd";
    }

    @ResponseBody
    @RequestMapping("/computerMethodAdd")
    public String articleAdd(@ModelAttribute ComputerMethod computerMethod) {
        try {
            computerMethod.setCreateTime(new Date());
            computerMethod.setModifiedTime(new Date());
            computerMethodService.insert(computerMethod);
            return SUCCESS_MSG;
        }catch (Exception e){
            return ERROR_MSG;
        }
    }

    @RequestMapping(value = "/toComputerMethodUpdate/{id}")
    public String toComputerMethodUpdate(Model model, @PathVariable Integer id) {
       ComputerMethod computerMethod = computerMethodService.selectById(id);
        model.addAttribute("computerMethod", computerMethod);
        return "manage/computerMethodUpdate";
    }

    @ResponseBody
    @RequestMapping(value = "/computerMethodUpdate",method = RequestMethod.POST)
    public String computerUpdate(@ModelAttribute ComputerMethod computerMethod) {
        try {
            computerMethod.setModifiedTime(new Date());
            computerMethodService.updateById(computerMethod);
            return SUCCESS_MSG;
        }catch (Exception e){
            return ERROR_MSG;
        }

    }




}
